<template>
  <el-button type="success" @click="user.dialogVisible.value = true">创建</el-button>

  <Table v-model:data="user.data.value" v-model:diag="user.dialogVisible.value">
    <el-table-column label="用户名" prop="username"/>
    <el-table-column label="角色" prop="role"/>
    <el-table-column fixed="right" label="操作" width="200px">
      <template #default="scope">
        <el-button link type="primary" size="small" @click="modiy(scope.row)">编辑</el-button>
        <el-button link type="danger" size="small" @click="user.Delete(scope.row.id)">删除</el-button>
        <el-button link type="warning" size="small" @click="user.Delete(scope.row.id)">修改密码</el-button>
        <el-button link type="success" size="small">Token</el-button>
      </template>
    </el-table-column>
  </Table>
  <Pagination @change="user.List" v-model:page="user.pages.value"/>


  <Dialog v-model:diag="user.dialogVisible.value" title="用户创建" @submit="OnSubmit(ruleFormRef)">
    <el-form :model="form" label-width="120px" ref="ruleFormRef" :rules="rules" status-icon>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username"/>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password" show-password/>
      </el-form-item>

      <el-form-item label="角色">
        <el-select v-model="form.role" placeholder="角色">
          <el-option v-for="item in projects" :key="item" :label="item" :value="item"/>
        </el-select>
      </el-form-item>

      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email"/>
      </el-form-item>
    </el-form>
  </Dialog>

  <Dialog v-model:diag="modifyUserDia" title="用户修改" @submit="OnModify(ruleFormRef)">
    <el-form :model="form" label-width="120px" ref="ruleFormRef" :rules="rules" status-icon>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username"/>
      </el-form-item>

      <el-form-item label="角色">
        <el-select v-model="form.role" placeholder="角色">
          <el-option v-for="item in projects" :key="item" :label="item" :value="item"/>
        </el-select>
      </el-form-item>

      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email"/>
      </el-form-item>
    </el-form>
  </Dialog>
</template>

<script setup lang="ts">
import {user} from '@/api/base'
import type {FormInstance, FormRules} from 'element-plus'
import {onMounted, reactive, ref} from 'vue'
import Pagination from '../../../components/common/Pagination.vue'
import Dialog from '@/components/common/Dialog.vue'
import Table from '@/components/common/Table.vue'

const modifyUserDia = ref(false)
const ruleFormRef = ref<FormInstance>()
const projects = ref(["超级管理员", "管理员", "成员"])

interface RuleForm {
  username: string
  password: string
  email: string
  role: string
}

const form = reactive(<RuleForm>{
  username: "",
  password: "",
  email: "",
  role: ""
})

const rules = reactive<FormRules>({
  username: [
    {required: true, message: '请输入有效的用户名', trigger: 'blur'},
    {min: 3, max: 16, message: '用户名需要3-16个字符', trigger: 'blur'},
  ],
  password: [
    {
      required: true,
      message: '密码不能为空',
      trigger: 'change',
    },
  ],
  role: [{
    required: true,
    message: '角色',
    trigger: 'change',
  }],
  email: [
    {
      required: true,
      message: '邮箱',
      trigger: 'change',
    },
  ]
})

onMounted(async () => {
  await user.List()
})

const user_id = ref()
const modiy = (user) => {
  modifyUserDia.value = true
  form.role = user.role
  form.username = user.username
  form.email = user.email
  user_id.value = user.id
}


const OnSubmit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid, fields) => {
    if (valid) {
    } else {
      return
    }
  })
  await user.Create(form)
}

const OnModify = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid, fields) => {
    if (valid) {
    } else {
      return
    }
  })
  form.id = user_id.value
  delete form.password
  await user.Modify(form)
}


</script>


<style scoped lang="scss">
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
